package com.hjw.service.impl;

import com.hjw.dao.HotDao;
import com.hjw.entity.Hot;
import com.hjw.service.HotService;
import com.hjw.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 * @创建时间：2025-04-14
 * @文件名称：satoken_sobuk - com.hjw.service.impl
 */
@Service
public class HotServiceImpl implements HotService {

    @Autowired
    private HotDao hotDao;
    @Autowired
    private RedisTemplate<String, Hot> redisTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public int add(Hot hot) {
        redisTemplate.delete("hot:"+hot.getId());
        int add = hotDao.add(hot);
        redisTemplate.delete("hot:"+hot.getId());
        return add;
    }

    @Override
    public int delete(int id) {
        redisTemplate.delete("hot:"+id);
        int delete = hotDao.delete(id);
        redisTemplate.delete("hot:"+id);
        return delete;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public int update(Map<String, Object> map) {
        redisTemplate.delete("hot:"+map.get("id"));
        int update = hotDao.update(map);
        redisTemplate.delete("hot:"+map.get("id"));
        return update;
    }

    @Override
    public List<Hot> findAll() {
        return hotDao.findAll();
    }

    @Override
    public List<Hot> findByParams(Map<String, Object> map) {
        return hotDao.findByParams(map);
    }

    @Override
    public Hot findById(int id) {
        Hot hot = redisTemplate.opsForValue().get("hot:" + id);
        if (hot != null) {
            return hot;
        }
        hot = hotDao.findById(id);
        if (hot != null){
            redisTemplate.opsForValue().set("hot:" + id, hot, 7200, TimeUnit.SECONDS);
        }
        return hot;
    }
}
